package is.glif.fenius.webui.client;


import java.io.File;
import java.util.HashMap;

import org.apache.cxf.helpers.FileUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class FileCredentialsProvider implements CredentialsProvider {
    private static Logger log = Logger.getLogger(FileCredentialsProvider.class);
    private static FileCredentialsProvider instance;
    public static FileCredentialsProvider getInstance() {
        if (instance == null) {
            instance = new FileCredentialsProvider();
        }
        return instance;
    }
    private FileCredentialsProvider() {
        this.refresh();
    }
    private String directory = "./config/credentials"; 
    private HashMap<String, Long> lastLoaded = new HashMap<String, Long>();
    private HashMap<String, String> fileContents = new HashMap<String, String>();


    public String getUsername(String network) throws JSONException {
        return (String) this.getValue(network, "username");
    }
    
    public String getPassword(String network) throws JSONException {
        return (String) this.getValue(network, "password");
    }
    
    public String getURL(String networkId) throws JSONException {
        return (String) this.getValue(networkId, "url");
    }
    
    public Boolean isHttpBasicAuth(String networkId) throws JSONException {
        return (Boolean) this.getValue(networkId, "httpBasicAuth");
    }
    
    public Boolean isSSL(String networkId) throws JSONException {
        return (Boolean) this.getValue(networkId, "ssl");
    }
    
    private void refresh() {
        Long now = System.currentTimeMillis();
        File folder = new File(directory);
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            File file = listOfFiles[i];
            if (file.isFile()) {
                String fileName = file.getName();
                Long modified = file.lastModified();
                boolean reload = false;
                if (!lastLoaded.containsKey(fileName)) {
                    reload = true;
                } else if (lastLoaded.get(fileName) < modified) {
                    reload = true;
                }
                log.debug(fileName+" last:"+lastLoaded.get(fileName)+" mod:"+modified);
                if (reload) {
                    log.debug("reloading "+fileName);
                    String jsonString = FileUtils.getStringFromFile(file);
                    lastLoaded.put(fileName, now);
                    fileContents.put(fileName, jsonString);
                }
            }
        }
    }
    
    
    private Object getValue(String network, String key) throws JSONException {
        this.refresh();
        for (String jsonString : fileContents.values()) {
            JSONArray tmp = new JSONArray(jsonString);
            for (int j  = 0; j < tmp.length(); j++) {
                JSONObject obj = tmp.getJSONObject(j);
                String thisNet = obj.getString("network");
                Object value = obj.get(key);
                if (thisNet.equals(network)) {
                    return value;
                }
            }
        }
        return null;
    }

}
